<template>
            <section>
              <!--工具条-->
              <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="query">
                  <el-form-item>
                    <el-input v-model="query.keyword" placeholder="部门名称"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" v-on:click="getQueryPermissions">查询</el-button>
				</el-form-item>

			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="权限名称" width="200" sortable>
			</el-table-column>
			<el-table-column prop="parent.name" label="父权限" width="200" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="sn" width="200" sortable>
			</el-table-column>
      <el-table-column prop="descs" label="描述" min-width="180" sortable>
      </el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
<!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">-->
<!--			</el-pagination>-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.query.currentPage"
          :page-sizes="[5,10,15,20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.pageInfo.total"
          style="float:right">
      </el-pagination>

		</el-col>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				query: {
					keyword: '',
          currentPage:1,
          pageSize:5
				},
        pageInfo:{
          dataList: [],
          total: 0,
        },
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入部门名字', trigger: 'blur' }
					]
				},
				//编辑界面数据
				// editForm: {
				// 	id: 0,
				// 	name: '',
				// 	sex: -1,
				// 	age: 0,
				// 	birth: '',
				// 	addr: ''
				// },

				saveFormVisible: false,//新增界面是否显示
				addLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
				  id:null,
          name:"",
          intro:"",
          manager:{
				    id:null,
            username:""
          },
          parent:{
				    id:null,
            name:""
          },
          state:""
				},
        employees:[],
        //定义父部门数据
        deptTree:[]
			}
		},
		methods: {
		  // getEmployees(){
      //   this.$http.get("/employee")
      //       .then(result => {
      //         result = result.data
      //         if (result.success) {
      //           this.employees=result.resultObj
      //         } else {
      //         }
      //       })
      //       .catch(result => {
      //       })
      // } ,
		  handleSizeChange(val){

		    this.query.pageSize=val;
		    this.getPermissions()
      },
		  getQueryPermissions(){
		    this.query.currentPage=1
        this.getPermissions()
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},

      //分页显示转换为第1页
			handleCurrentChange(val) {
        console.log(val);
				this.query.currentPage = val;
        this.getPermissions()
      },
			//获取部门列表
			getPermissions() {
				// let para = {
				// 	page: this.page,
				// 	name: this.filters.name
				// };
				this.listLoading = true;
				//NProgress.start();
        this.$http.put("/permission",this.query)
            .then(result=>{
              console.log(result);
              result=result.data
             if (result.success){
               this.$message({
                 message: '查询成功',
                 type: 'success'
               });
               this.listLoading = false;
               console.log(result);
               this.pageInfo=result.resultObj
               console.log(this.pageInfo.dataList)
             }else{
               this.$message({
                 message: '查询失败',
                 type: 'danger'
               });
             }
            })
            .catch(result=>{

            })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/permission/"+row.id)
          .then(result=>{
            result=result.data
            if (result.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.listLoading = false;
              this.query.currentPage=1
              this.getPermissions()
            }else{
              this.$message({
                message: '删除失败',
                type: 'danger'
              });
            }
          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
        this.getEmployees()
        this.getDeptTree()
				this.saveForm = Object.assign({}, row)
        if (!row.parent){
          this.saveForm.parent={
            id:null,
            name:""
          }
        }
        if (!row.manager){
          this.saveForm.manager={
            id:null,
            name:""
          }
        }
        //处理级联下拉框回显
        var path = row.path;
        var split=path.split("/")
        var p=[]
        for (let i=1;i<split.length-1;i++){
          p.push(Number.parseInt(split[i]))
          this.saveForm.parent=p
        }

        this.saveForm.state=Number.parseInt(row.state)
			},
			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveForm = {
          id:null,
          name:"",
          intro:"",
          manager:{
            id:null,
            username:"",
          },parent:{
            id:null,
            name:""
          },
          state:""
        }
        this.getEmployees()
        this.getDeptTree()
			},
			//新增
			addSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {})
                .then(() => {
                  this.addLoading = true;
                  //NProgress.start();
                  let para = Object.assign({}, this.saveForm);
                  //处理父级级联框
                  var parent = this.saveForm.parent;
                  if (parent&&parent.length>0){
                    var id=parent[parent.length-1]
                    para.parent={
                      id:id
                    }
                  }
                  this.$http.post("/permission", para)
                      .then(result => {
                        result = result.data
                        if (result.success) {
                          this.$message({
                            message: '保存成功',
                            type: 'success'
                          });
                          this.addLoading = false;
                          this.saveFormVisible=false;
                          this.getPermissions()
                        } else {
                          this.$message({
                            message: '保存失败',
                            type: 'danger'
                          });
                        }
                      })
                      .catch(result => {
                        this.$message({message: "网络异常，请检查网络", type: "danger"})
                      })
                       }).catch({})
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
          //NProgress.start();
          this.$http.patch("/permission",ids)
              .then(result => {
                result = result.data
                if (result.success) {
                  this.$message({
                    message: '批量删除成功',
                    type: 'success'
                  });
                  this.listLoading = false;
                  this.query.currentPage = 1
                  this.getPermissions()
                } else {
                  this.$message({
                    message: '批量删除失败',
                    type: 'danger'
                  });
                }
              })
              .catch(result=>{

                })
              })
              .catch(() => {

              });
        }
		},
		mounted() {
			this.getPermissions();
		}
	}

</script>

<style scoped>

</style>